<?php use_stylesheets_for_form($form) ?>
<?php use_javascripts_for_form($form) ?>
<style type="text/css">
.qra {
 color: #a80000;
 text-decoration: none;
 font-size: 12px;
 font-family: Verdana, sans-serif;
}

.qra:hover {
  border: 1px dotted #a80000;
}
</style>
<script type="text/javascript">

$(function() {
  $('#copy-button').click(function() {
    var address = '<?php echo url_for("questionaire") ?>/copy?id=<?php echo $sf_request->getParameter("id") ?>';
    
    // Address is something like this: /backend.php/questionaire/copy?id=123
    location.href = address;
  });
});

</script>


<div class="sf_admin_form">
    <?php echo form_tag_for($form, '@' . $helper->getIndexRoute()) ?>
    <?php echo $form->renderHiddenFields(true) ?>

    <?php if ($form->hasGlobalErrors()): ?>
        <?php echo $form->renderGlobalErrors() ?>
    <?php endif; ?>

    <table width="50%" style="float:left">
        <tr>
            <td width="50%">
                <fieldset>
                    <legend class="formLabel"><?php echo __('Questionaire information') ?></legend>
                    <table>
                        <?php foreach (questionaireForm::$multiLangFields as $field => $fieldInfo): ?>
                            <?php foreach ($form->languages as $lang): ?>
                                <tr>
                                    <td>
                                        <?php echo $form['et'][$field]->renderLabel($fieldInfo['title'] . " (${lang['abr']})", array('class' => 'formLabel')) ?>
                                    </td>
                                    <td>
                                        <?php echo $form[$lang['abr']][$field]->render(array()) ?>
                                    </td>

                                </tr>
                            <?php endforeach; ?>
                        <?php endforeach; ?>
                        <tr>
                            <td><?php echo $form['answers_amt']->renderLabel('', array('class' => 'formLabel')) ?> </td>
                            <td><?php echo $form['answers_amt']->render(array('class' => 'formInput')) ?> </td>
                            <td><?php echo $form['answers_amt']->renderError() ?> </td>
                        </tr>
                    </table>
                </fieldset>
            </td>
        </tr>
        <tfoot style="height:50px;">
            <tr>
                <td colspan="2">
                    <input class="formSubmit" type="submit" name="Save" value="<?php echo __('Save') ?>" />
                    <?php if (!$form->isNew()): ?>
                        <input style="float:right;" class="formSubmit" type="button" id="copy-button" name="Copy" value="<?php echo __('Copy') ?>" />
                    <?php endif; ?>
                </td>
            </tr>
        </tfoot>        
    </table>
    <table>
        <tr>
            <td width="50%" >
                <fieldset>
                    <legend class="formLabel"><?php echo __('Additional information') ?></legend>
                    <table width="100%" height="100%">
                        <tr>
                            <td><?php echo $form['structure_id']->renderLabel('Client structure connection', array('class' => 'formLabel')) ?></td>
                            <td>
                                <?php echo $form['structure_id'] ?>
                            </td>
                        </tr>
                        <?php if (!$form->isNew()): ?>
                            <tr><td>&nbsp;</td></tr>
                            <tr>
                                <td><span class="formLabel"><?php echo __('Status') ?></span></td>
                                <?php $isActive = $questionaire->getIsActive(); ?>
                                <?php $status = $isActive ? 'activated' : 'deactivated' ?>
                                <?php $time = $isActive ? ' since '. $questionaire->getActiveFrom() : '' ?>
                                <td><?php echo __($status).$time ?></td>
                                <?php if ($isActive): ?>
                                    <td><?php echo __('Answered')?>: <?php echo $questionaire->getAnsweredCt(); ?></td>
                                <?php endif; ?>
                            </tr>
                            <?php if ($isActive): ?>
                                <?php // Very very ugly, must fix ?>
                                <?php $children = $questionaire->getStructure()->getStructureChilds(); ?>
                                <?php foreach ($children as $child): ?>
                                    <?php if ($child->getParameter() == 'questionaire'): ?>
                                        <?php $qSlug = $child->getSlug(); ?>
                                        <?php break; ?>
                                    <?php endif; ?>
                                <?php endforeach; ?>
                                <?php $url = $sf_request->getUriPrefix().'/'.$questionaire->getStructure()->getLang().'/'.$questionaire->getStructure()->getSlug() ?>
                                <tr><td>&nbsp;</td></tr>
                                <tr>
                                    <td><span class="formLabel"><?php echo __('URL') ?></span></td>
                                    <td><input class="formInput" readonly="readonly" type="text" value="<?php echo $url ?>" /></td>
                                </tr>
                                  
                                <tr>
                                    <td><?php echo $form['active_from']->renderLabel('', array('class' => 'formLabel')) ?> </td>
                                    <td><?php echo $form['active_from']->render(array('class' => 'formInput')) ?> </td>
                                    <td><?php echo $form['active_from']->renderError() ?> </td>
                                </tr>
                                
                                <tr>
                                    <td><?php echo $form['active_until']->renderLabel('', array('class' => 'formLabel')) ?> </td>
                                    <td><?php echo $form['active_until']->render(array('class' => 'formInput')) ?> </td>
                                    <td><?php echo $form['active_until']->renderError() ?> </td>
                                </tr>
                                
                                <tr><td>&nbsp;</td></tr>
                                <tr>
                                    <td><span class="formLabel"><?php echo __('QRCode') ?></span></td>
                                    <td><img src="http://chart.apis.google.com/chart?cht=qr&chs=150x150&chl=<?php echo $url ?>&chld=H|0" />
                                      <div style="float: right">
                                        <div><a href="http://chart.apis.google.com/chart?cht=qr&chs=250x250&chl=<?php echo $url ?>&chld=H|0" target="_blank" class="qra">[ M ]</a></div>
                                        <div><a href="http://chart.apis.google.com/chart?cht=qr&chs=350x350&chl=<?php echo $url ?>&chld=H|0" target="_blank" class="qra">[ L ]</a></div>
                                        <div><a href="http://chart.apis.google.com/chart?cht=qr&chs=450x450&chl=<?php echo $url ?>&chld=H|0" target="_blank" class="qra">[ XL ]</a></div>
                                      </div>
                                    </td>
                                </tr>
                            <?php endif; ?>
                        <?php endif; ?>
                    </table>
                </fieldset>
            </td>
        </tr>
    </table>
</form>
</div>

